<%--
  Created by IntelliJ IDEA.
  User: 27716
  Date: 2024/10/29
  Time: 10:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>列表</title>
    <script src="../../static/js/jquery-1.8.2.min.js"></script>
    <link rel="stylesheet" href="../../static/css/css.css">
    <script>
        $(function (){
            let supplierName = $("#supplierName").val();
            let obj = {supplierName:supplierName};
            getSupplierList(obj);
        })
        function getSupplierList(obj){
            $.ajax({
                url:"/supplier/getSupplierList",
                type:"post",
                data:{reqInfos:JSON.stringify(obj)},
                dataType:"json",
                success(res){
                    console.log(res);
                    let arr = res.list;
                    $("#table").empty();
                    $("#table").append(`
                        <tr>
                            <td>
                                <input type="checkbox" id="check">
                            </td>
                            <td>供应商id</td>
                            <td>供应商名称</td>
                            <td>联系人</td>
                            <td>联系人电话</td>
                            <td>供应商地址</td>
                            <td>备注</td>
                            <td>操作</td>
                        </tr>
                    `)
                    for (let o of arr) {
                        $("#table").append(`
                        <tr>
                            <td>
                                <input type="checkbox" class="ck" value="\${o.supplierId}">
                            </td>
                            <td>\${o.supplierId}</td>
                            <td>\${o.supplierName}</td>
                            <td>\${o.contacts}</td>
                            <td>\${o.phoneNumber}</td>
                            <td>\${o.address}</td>
                            <td>\${o.remarks}</td>
                            <td>
                                <input type="button" value="删除" onclick="supplierDel(\${o.supplierId})">
                                <input type="button" value="修改" onclick="supplierUpd(\${o.supplierId})">
                            </td>
                        </tr>
                        `)
                    }
                    $("#table").append(`
                        <tr>
                            <td colspan="100">
                                <input type="button" value="首页" onclick="doPage(1)">
                                <input type="button" value="上一页" onclick="doPage(\${res.prePage})">
                                <input type="button" value="下一页" onclick="doPage(\${res.nextPage})">
                                <input type="button" value="尾页" onclick="doPage(\${res.pages})">
                            </td>
                        </tr>
                    `)
                },
                error(){
                    alert("服务器错误")
                }
            })
        }

    </script>
</head>
<body>
<input type="text" id="supplierName">
<input type="button" value="搜索" onclick="search()">
<input type="button" value="批删除" onclick="toGetAllDel()">
<input type="button" value="添加" onclick="toGetAdd()">
<table id="table">
    <tr>
        <td>供应商id</td>
        <td>供应商名称</td>
        <td>联系人</td>
        <td>联系人电话</td>
        <td>供应商地址</td>
        <td>备注</td>
    </tr>
</table>
</body>
<script>
    //添加
    function toGetAdd(){
        location.href = "add.jsp";
    }
    //删除
    function supplierDel(supplierId){
        let obj = {supplierId:supplierId};
        $.ajax({
            url:"/supplier/getSupplierDel",
            type:"post",
            data: {reqInfos:JSON.stringify(obj)},
            dataType: "json",
            success(res) {
                if(res === 200){
                    alert("删除成功");
                    location.reload();
                }
            },
            error() {
                alert("服务器错误");
            }
        })
    }
    //批删除
    function toGetAllDel(){
        let arr= [];
        $(".ck:checked").each(function (){
            let uid = $(this).val();
            arr.push(uid);
        })
        let supplierId = arr.join(",");
        if(!supplierId){
            alert("数据不能为空");
            return;
        }
        supplierDel(supplierId);
    }
    //全选
    $(document).on('click',"#check",function (){
        $(".ck").prop("checked",this.checked);
    })

    //搜索框
    function search(){
        let supplierName = $("#supplierName").val();
        let obj = {supplierName:supplierName};
        getSupplierList(obj);
    }
    //页数
    function doPage(pageNum){
        let supplierName = $("#supplierName").val();
        let obj = {supplierName:supplierName,pageNum:pageNum};
        getSupplierList(obj);
    }
    //修改
    function supplierUpd(supplierId){
        location.href = "upd.jsp?supplierId="+supplierId;
    }
</script>
</html>
